<template>
  <!-- 监控事件配置新增-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="addForm" :model="addForm" :rules="rules" label-width="110px">
      <!--监控事件配置新增-->
      <el-card class="form-card monitorConf-card">
        <div slot="header" class="label-header-msg">
          <span></span>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="事件名称" prop="eventNm">
              <el-input
                v-model="addForm.eventNm"
                placeholder="事件名称"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="业务类型" prop="bussType">
              <el-input
                v-model="addForm.bussType"
                placeholder="业务类型"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="监控类型" prop="monitorType">
              <el-select
                v-model="addForm.monitorType"
                placeholder="监控类型"
                style="width: 100%"
                @change="monitorTypeChange"
              >
                <el-option
                  v-for="dict in dictMap.MONITOR_TYPE"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="超时时间" prop="timeoutLimit" v-if="timeoutLimitShow">
              <el-input
                v-model="addForm.timeoutLimit"
                placeholder="超时时间"
              />
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="监控周期" prop="monitorCycle">-->
<!--              <el-select-->
<!--                v-model="addForm.monitorCycle"-->
<!--                placeholder="监控周期"-->
<!--                style="width: 100%"-->
<!--              >-->
<!--                <el-option-->
<!--                  v-for="dict in dictMap.MONITOR_CYCLE "-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.text"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

        <el-row>
          <el-col :span="24">
            <el-form-item label="执行SQL" prop="executeSql">
              <el-input
                v-model="addForm.executeSql"
                placeholder="执行SQL"
                type="textarea"
                autosize
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="SQL可选条件" prop="sqlCon">
              <el-select
                v-model="addForm.sqlCondition"
                clearable
                placeholder="SQL可选条件"
                style="width: 100%"
                @change = "sqlConChange"
              >
                <el-option
                  v-for="item in this.conditionOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">提 交</el-button>
      <el-button type="primary" @click="reset">重 置</el-button>
      <el-button type="primary" @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { monitorConfAdd} from "@/api/cnaps/bizMng/monitorConf.js";
import { listPageMixin } from "@/libs/listPageMixin";

export default {
  name: "monitorConfAddDialog",
  mixins: [listPageMixin],
  components: {
  },
  props:{
    conditionOptions: Array
  },
  data() {
    return {
      title: "",
      // 是否显示弹出层
      openState: false,
      timeoutLimitShow: false,
      // 字段可编辑控制

      //数据字典
      searchFormDictList: [
       "MONITOR_CYCLE",
       "MONITOR_TYPE"
      ],
      // 表单参数
      addForm:{},
      // 表单校验
      rules: {
        eventNm: [{ required: true, message: "请输入事件名称", trigger: "blur" }],
        bussType: [{ required: true, message: "请输入业务类型", trigger: "blur" }],
        monitorType: [{ required: true, message: "请选择监控类型", trigger: "blur" }],
        timeoutLimit: [{ required: false, message: "请输入超时时间", trigger: "blur" }],
        executeSql: [{ required: true, message: "请输入执行SQL", trigger: "blur" }],
        // monitorCycle: [{ required: true, message: "请选择监控周期", trigger: "blur" }],
      },
      opButtonList: () => {
        return [
        ];
      },
    };
  },
  mounted() {
  },
  methods: {
    // 表单重置
    reset() {
      this.resetForm("addForm");
      this.$set(this, "addForm", {});
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.reset();
          done();
        })
        .catch(_ => {});
    },
    show() {
      this.title = "监控事件配置新增";
      this.openState = true;

      this.monitorTypeChange();
    },

    /** TODO提交按钮 */
    submitForm: function() {
      this.$refs["addForm"].validate(valid => {
        if (valid) {
          this.submit();
        }
      });
    },

    submit() {
      monitorConfAdd(this.addForm).then(res => {
        if (res.code === 'S') {
          this.msgSuccess("提交成功");
          this.cancel();
        }
      });
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", true);
    },

    monitorTypeChange() {
      let monitorType = this.addForm.monitorType;
      if(monitorType=='02'){ //超时

        this.timeoutLimitShow = true;
        this.rules.timeoutLimit= {
          required: true,
          message: "请输入超时时间",
          trigger: "blur",
        }
      }else{ //发送失败或记账失败
        this.$set(this.addForm, "timeoutLimit", "");
        this.timeoutLimitShow = false;
        this.rules.timeoutLimit= {
          required: false,
          message: "请输入超时时间",
          trigger: "blur",
        }
      }
    },
    sqlConChange(){
      let sql = this.addForm.sqlCondition;
      let executeSql = this.addForm.executeSql;
      if (executeSql !== null && typeof executeSql !== "undefined") {
        this.$set(this.addForm, "executeSql", executeSql+sql);
      }else {
        this.$set(this.addForm, "executeSql", sql);
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
